package com.caiheng.api.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.*;

/**
 * 说明：日期处理
 * 创建人：zw
 * 修改时间：2015年11月24日
 * @version
 */
public class DateUtil {

	public final static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
	public final static SimpleDateFormat sdfMonth1 = new SimpleDateFormat("MM");
	public final static SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM");
	public final static SimpleDateFormat sdfMonthSimple = new SimpleDateFormat("yyyyMM");
	public final static SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
	public final static SimpleDateFormat sdfMonthDay = new SimpleDateFormat("MM-dd");
	public final static SimpleDateFormat sdfDayCN = new SimpleDateFormat("yyyy年MM月dd日");
	public final static SimpleDateFormat sdfYearDay = new SimpleDateFormat("yyyy/MM/dd");
	public final static SimpleDateFormat sdfDay1 = new SimpleDateFormat("yy/MM/dd");
	public final static SimpleDateFormat sdfDay2 = new SimpleDateFormat("yy-MM-dd");
	public final static SimpleDateFormat sdfDays = new SimpleDateFormat("yyyyMMdd");
	private final static SimpleDateFormat sdfSimpleTime = new SimpleDateFormat("HH:mm:ss");
	private final static SimpleDateFormat sdfSimpleTime1 = new SimpleDateFormat("HH:mm");
	public final static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public final static SimpleDateFormat sdfTime1 = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
	public final static SimpleDateFormat sdfTimes = new SimpleDateFormat("yyyyMMddHHmmss");
	public final static SimpleDateFormat sdfffTimes = new SimpleDateFormat("yyyyMMddHHmmss");
	public final static SimpleDateFormat sdfTime4 = new SimpleDateFormat("MM月dd日 HH:mm");

	/**
	 * 获取yyyyMMddHHmmss格式
	 * @return
	 */
	public static String getSdfTimes() {
		return sdfTimes.format(new Date());
	}

	/**
	 * 获取yyyy年MM月dd日格式
	 * @return
	 */
	public static String getSdfTimesCN() {
		return sdfDayCN.format(new Date());
	}


	/**
	 * 格式化日期
	 *
	 * @return
	 */
	public static Date fomatTime(String date) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return fmt.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static Date getDateByMinuts(Date date, int minuts) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		if (minuts > 0) {
			calendar.add(Calendar.MINUTE, minuts);
		}
		return calendar.getTime();
	}


	public static Date getDateByDays(Date date, int days) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		if (days > 0) {
			calendar.add(Calendar.DATE, days);
			calendar.set(Calendar.HOUR_OF_DAY,23);
			calendar.set(Calendar.MINUTE,59);
			calendar.set(Calendar.SECOND,59);
		}
		return calendar.getTime();
	}

	/**
	 * 获取yyyyMMddHHmmss格式
	 * @return
	 */
	public static String getTimesStr(Date date) {
		return sdfDay.format(date);
	}

	/**
	 * 获取yyyyMMddHHmmss格式
	 * @return
	 */
	public static String getMonthStr(Date date) {
		return sdfMonthSimple.format(date);
	}

	public static String getMonthDay(Date date) {
		return sdfMonthDay.format(date);
	}


	/**
	 * 获取YYYY-MM-DD HH:mm:ss格式
	 * @return
	 */
	public static String getSimpleTime(Date date) {
		return sdfSimpleTime.format(date);
	}

	/**
	 * 获取YYYY-MM-DD HH:mm:ss格式
	 * @return
	 */
	public static String getSimpleTime1(Date date) {
		return sdfSimpleTime1.format(date);
	}


	/**
	 * 获取yyyyMMddHHmmss格式
	 * @return
	 */
	public static String getTimesStr(Date date,SimpleDateFormat sdf) {
		return sdf.format(date);
	}


	/**
	 * 获取YYYY格式
	 * @return
	 */
	public static String getSdfffTimes() {
		return sdfffTimes.format(new Date());
	}



	/**
	 * 获取YYYY格式
	 * @return
	 */
	public static String getSdfffTimes(Date data) {
		return sdfffTimes.format(data);
	}


	/**
	 * 获取时间的 年月日时分秒
	 * @param date
	 * @param timeCode  Y-年 M-月份 D- 日期  H- 小时 m:分钟 s - 秒数  wfy - 一周的第几天
	 * @return
	 */
	public static int getTimeByTimeCode(Date date,String timeCode){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int ret = -1;
		switch (timeCode){
			case "Y": return cal.get(Calendar.YEAR);
			case "M": return cal.get(Calendar.MONTH);
			case "D": return cal.get(Calendar.DAY_OF_MONTH);
			case "H": return cal.get(Calendar.HOUR_OF_DAY);
			case "m": return cal.get(Calendar.MINUTE);
			case "s": return cal.get(Calendar.SECOND);
			case "wfy": return cal.get(Calendar.DAY_OF_WEEK);
		}
//		int year = cal.get(Calendar.YEAR);//获取年份
//		int month=cal.get(Calendar.MONTH);//获取月份
//		int day=cal.get(Calendar.DATE);//获取日
//		int hour=cal.get(Calendar.HOUR);//小时
//		int minute=cal.get(Calendar.MINUTE);//分
//		int second=cal.get(Calendar.SECOND);//秒
//		int WeekOfYear = cal.get(Calendar.DAY_OF_WEEK);//一周的第几天
		return ret;
	}

	/**
	 * 获取YYYY格式
	 * @return
	 */
	public static String getYear() {
		return sdfYear.format(new Date());
	}

	/**
	 * 获取MM格式
	 * @return
	 */
	public static String getSdfYearDay() {
		return sdfYearDay.format(new Date());
	}

	/**
	 * 获取YYYY-MM-DD格式
	 * @return
	 */
	public static String getDay() {
		return sdfDay.format(new Date());
	}

	public static Date getDateByStr(String date) {
		Date d2 = null;
		try {
			d2 = sdfTime.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d2;
	}

	/**
	 * 获取今天的0点时间
	 * @return
	 */
	public static Date getTodayZero(){
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.set(Calendar.HOUR_OF_DAY,0);
		cal.set(Calendar.MINUTE,0);
		cal.set(Calendar.SECOND,0);
		return cal.getTime();
	}


	public static Date getTodayLast() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.HOUR_OF_DAY,23);
		calendar.set(Calendar.MINUTE,59);
		calendar.set(Calendar.SECOND,59);
		return calendar.getTime();
	}

	/**
	 * 获取MM-dd HH:mm:ss格式
	 * @return
	 */
	public static String getSdfTime4(Date date) {
		return sdfTime4.format(date);
	}

	public static String getDateStr(Date createTime){
		Date dateZero = DateUtil.getTodayZero();
		if(createTime.getTime() >= dateZero.getTime()){
			return DateUtil.getSimpleTime(createTime);
		} else{
			return DateUtil.getSdfTime4(createTime);
		}
	}

	/**
	 * 获取YYYY-MM-DD格式
	 * @return
	 */
	public static String getDayByDate(Date date) {
		return sdfDay.format(date);
	}

	/**
	 * 获取当前日期是星期几<br>
	 *
	 * @param date
	 * @return 当前日期是星期几
	 */
	public static String getWeekOfDate(Date date) {
		String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return weekDays[w];
	}


	/**
	 *  获取当期时间是否出在 晚8点到早8点之间
	 *  1  为在次时间段 就是晚上  0 - 为在白天时间段
	 * @return
	 */
	public static int dayOrNight(){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
//		System.out.println("hour:" + hour);
		if((hour >= 20 && hour <= 23) || (hour >= 0 && hour <= 7)){
			return 1;
		}else{
			return 0;
		}
	}

	/**
	 * 获取当前日期是星期几<br>
	 *
	 * @param date
	 * @return 当前日期是星期几   周日 0 周六  6
	 */
	public static int getWeekIndexOfDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return w;
	}

	public static Date getTimeAfterYear(int year){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.YEAR,year);
		calendar.set(Calendar.HOUR_OF_DAY,23);
		calendar.set(Calendar.MINUTE,59);
		calendar.set(Calendar.SECOND,59);
		return calendar.getTime();
	}


	public static Map<String,Object> getTodayTime(){
		Map<String,Object> map = new HashMap<>();

		long current=System.currentTimeMillis();//当前时间毫秒数
		long zero=current/(1000*3600*24)*(1000*3600*24)- TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
		long twelve=zero+24*60*60*1000-1;//今天23点59分59秒的毫秒数
		long yesterday=System.currentTimeMillis()-24*60*60*1000;//昨天的这一时间的毫秒数
		System.out.println(new Timestamp(current));//当前时间
		System.out.println(new Timestamp(yesterday));//昨天这一时间点

		System.out.println(new Timestamp(zero));//今天零点零分零秒
		System.out.println(new Timestamp(twelve));//今天23点59分59秒
		map.put("begin_time",new Timestamp(zero));
		map.put("end_time",new Timestamp(twelve));
		return map;
	}

	/**
	 * 获取YYYY-MM-DD格式
	 * @return
	 */
	public static String getDay1() {
		return sdfDay1.format(new Date());
	}


	/**
	 *   将日期字符串  20240109121134   改成日期格式
	 * @param dateStr
	 * @return
	 */
	public static String getDateStrByString(String dateStr){
		String formattedDate = "";
		SimpleDateFormat sdfInput = new SimpleDateFormat("yyyyMMddHHmmss");
		Date date = null;
		try {
			date = sdfInput.parse(dateStr);

			SimpleDateFormat sdfOutput = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			formattedDate = sdfOutput.format(date);

			System.out.println(formattedDate);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}

		return formattedDate;
	}


	/**
	 *
	 * @param dateStr
	 * @return
	 */
	public static Date getDateByString(String dateStr){
		String formattedDate = "";
		SimpleDateFormat sdfInput = new SimpleDateFormat("yyyyMMddHHmmss");
		Date date = null;
		try {
			date = sdfInput.parse(dateStr);

//			SimpleDateFormat sdfOutput = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//			formattedDate = sdfOutput.format(date);
//
//			System.out.println(formattedDate);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}

		return date;
	}

	/**
	 * 获取YY-MM-DD格式
	 * @return
	 */
	public static String getDay2() {
		return sdfDay2.format(new Date());
	}

	/**
	 * 获取YYYY-MM-DD格式
	 * @return
	 */
	public static String getDateByFormat(Date date,String format) {
		SimpleDateFormat sdfDays = new SimpleDateFormat(format);
		return sdfDays.format(date);
	}

	/**
	 * 获取YYYYMMDD格式
	 * @return
	 */
	public static String getDays(){
		return sdfDays.format(new Date());
	}

	/**
	 * 获取YYYY-MM-DD HH:mm:ss格式
	 * @return
	 */
	public static String getTime() {
		return sdfTime.format(new Date());
	}

	/**
	 * 获取YYYY-MM-DD HH:mm:ss格式
	 * @return
	 */
	public static String getTime(Date date) {
		return sdfTime.format(date);
	}

	/**
	 * 获取YY/MM/DD HH:mm:ss格式
	 * @return
	 */
	public static String getTime1() {
		return sdfTime1.format(new Date());
	}

	/**
	 * @Title: compareDate
	 * @Description: TODO(日期比较，如果s>=e 返回true 否则返回false)
	 * @param s
	 * @param e
	 * @return boolean
	 * @throws
	 * @author fh
	 */
	public static boolean compareDate(String s, String e) {
		if(formatDate(s)==null||formatDate(e)==null){
			return false;
		}
		return formatDate(s).getTime() >=formatDate(e).getTime();
	}


	/**
	 * Date 转 LocalDateTime
	 *
	 * @param date
	 * @return
	 */
	public static LocalDateTime dataToLocalDateTime(Date date) {
		if (date == null) {
			return null;
		}
		Instant instant = date.toInstant();
		ZoneId zoneId = ZoneId.systemDefault();
		return instant.atZone(zoneId).toLocalDateTime();
	}

	/**
	 * LocalDateTime 转 Date
	 *
	 * @param localDateTime
	 * @return
	 */
	public static Date localDateTimeToDate(LocalDateTime localDateTime) {
		if (localDateTime == null) {
			return null;
		}
		ZoneId zoneId = ZoneId.systemDefault();
		ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
		return Date.from(zonedDateTime.toInstant());
	}


	public static Date stampToDate(String date){
		SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy K:m:s a", Locale.ENGLISH);
		Date d2 = null;
		try {
			d2 = sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d2;
	}


	/**
	 *  得到近几天的日期
	 * @param days
	 * @return
	 */
	public static List<String> getDaysBetween(int days,String format){ //最近几天日期
		List<String> dayss = new ArrayList<>();
		Calendar start = Calendar.getInstance();
		start.setTime(getDateAdd(days));
		Long startTIme = start.getTimeInMillis();
		Calendar end = Calendar.getInstance();
		end.setTime(new Date());
		Long endTime = end.getTimeInMillis();
		Long oneDay = 1000 * 60 * 60 * 24l;
		Long time = startTIme;
		while (time <= endTime) {
			Date d = new Date(time);
			DateFormat df = new SimpleDateFormat(format);
			System.out.println(df.format(d));
			dayss.add(df.format(d));
			time += oneDay;
		}
		return dayss;
	}


	/**
	 *  得到日期范围内的日期
	 * @param beginDate
	 * @param endDate
	 * @return
	 */
	public static List<String> getDaysByDateBetween(String beginDate,String endDate,String format){ //最近几天日期
		List<String> dayss = new ArrayList<>();
		Calendar start = Calendar.getInstance();
		start.setTime(getDateByStr(beginDate));
		Long startTIme = start.getTimeInMillis();
		Calendar end = Calendar.getInstance();
		end.setTime(getDateByStr(endDate));
		Long endTime = end.getTimeInMillis();
		Long oneDay = 1000 * 60 * 60 * 24l;
		Long time = startTIme;
		while (time <= endTime) {
			Date d = new Date(time);
			DateFormat df = new SimpleDateFormat(format);
			System.out.println(df.format(d));
			dayss.add(df.format(d));
			time += oneDay;
		}
		return dayss;
	}


	private static Date getDateAdd(int days){
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, -days);
		return c.getTime();
	}


	/**
	 *  当前时间添加月数 几个月之后的时间
	 * @param months
	 * @return
	 */
	public static Date getDateAddMonths(int months){
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.MONTH, months);
		c.set(Calendar.HOUR_OF_DAY,23);
		c.set(Calendar.MINUTE,59);
		c.set(Calendar.SECOND,59);
		return c.getTime();
	}


	/**
	 * 格式化日期
	 * @return
	 */
	public static Date formatDate(String date) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return fmt.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static Date getDateTimeByStr(String str){
		Date date = null;
		if(str.length() == 14){
			String year = str.substring(0,4);
			String month = str.substring(4,6);
			String day = str.substring(6,8);


			String hour = str.substring(8,10);
			String min = str.substring(10,12);
			String sec = str.substring(12,14);
			String time = year + "-" + month + "-" + day+" "+ hour+":"+min+":"+sec;
			System.out.println("time: " + time);
			date = ConvertDateByformat(time,"yyyy-MM-dd HH:mm:ss");
		}
		return date;
	}

	/**
	 * 格式化日期
	 * @return
	 */
	public static String fomatDate(String dateStr,String format) {
		DateFormat fmt = new SimpleDateFormat(format);
		try {
			Date date = fmt.parse(dateStr);
			return sdfDay.format(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 格式化日期
	 * @return
	 */
	public static Date ConvertDateByformat(String date,String format) {
		DateFormat fmt = new SimpleDateFormat(format);
		try {
			return fmt.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}


	/**
	 * 格式化日期
	 * @return
	 */
	public static Date convertDateTime(String date) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return fmt.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 校验日期是否合法
	 * @return
	 */
	public static boolean isValidDate(String s) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		try {
			fmt.parse(s);
			return true;
		} catch (Exception e) {
			// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			return false;
		}
	}

	/**
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static int getDiffYear(String startTime,String endTime) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		try {
			//long aa=0;
			int years=(int) (((fmt.parse(endTime).getTime()-fmt.parse(startTime).getTime())/ (1000 * 60 * 60 * 24))/365);
			return years;
		} catch (Exception e) {
			// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			return 0;
		}
	}

	/**
     * <li>功能描述：时间相减得到天数
     * @param beginDateStr
     * @param endDateStr
     * @return
     * long
     * @author Administrator
     */
    public static long getDaySub(String beginDateStr,String endDateStr){
        long day = 0;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = null;
        Date endDate = null;

		try {
			beginDate = format.parse(beginDateStr);
			endDate= format.parse(endDateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		day = (endDate.getTime()-beginDate.getTime())/(24*60*60*1000);
		//System.out.println("相隔的天数="+day);

        return day;
    }

    /**
     *
     *@author by  zhouwei
     *@createtime: 2017/9/22 11:45
     *@Description：判断当前时间是否在次时间段内
     *@modifytime:   begintime endtime  格式  06：00
     *
     */
	public static boolean isBelongInTime(String begintime,String endtime){

		SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
		Date now = null;
		Date beginTime = null;
		Date endTime = null;
		try {
			now = df.parse(df.format(new Date()));
			beginTime = df.parse(begintime);
			endTime = df.parse(endtime);
		} catch (Exception e) {
			e.printStackTrace();
		}

		boolean flag = belongCalendar(now, beginTime, endTime);
		return flag;
	}

	/**
	 * 判断时间是否在时间段内
	 * @param nowTime
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
		Calendar date = Calendar.getInstance();
		date.setTime(nowTime);
		Calendar begin = Calendar.getInstance();
		begin.setTime(beginTime);
		Calendar end = Calendar.getInstance();
		end.setTime(endTime);
		if (date.after(begin) && date.before(end)) {
			return true;
		} else {
			return false;
		}
	}

    /**
     * 得到n天之后的日期
     * @param days
     * @return
     */
    public static String getAfterDayDate(Integer days) {
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.add(Calendar.DATE, days); // 日期减 如果不够减会将月变动
		canlendar.set(Calendar.HOUR_OF_DAY,23);
		canlendar.set(Calendar.MINUTE,59);
		canlendar.set(Calendar.SECOND,59);
        Date date = canlendar.getTime();
        SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdfd.format(date);
        return dateStr;
    }


	/**
	 * 得到n天之前的日期
	 * @param days
	 * @return
	 */
	public static String getBeforeDayDate(Integer days) {
		Calendar canlendar = Calendar.getInstance(); // java.util包
		canlendar.add(Calendar.DATE, days); // 日期减 如果不够减会将月变动
		canlendar.set(Calendar.HOUR_OF_DAY,0);
		canlendar.set(Calendar.MINUTE,0);
		canlendar.set(Calendar.SECOND,0);
		Date date = canlendar.getTime();
		SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = sdfd.format(date);
		return dateStr;
	}

	/**
	 * 两个时间之间相差距离多少天
	 * @param str1 时间参数 1：
	 * @param str2 时间参数 2：
	 * @return 相差天数
	 */
	public static long getDistanceDays(String str1, String str2) throws Exception{
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date one;
		Date two;
		long days=0;
		try {
			one = df.parse(str1);
			two = df.parse(str2);
			long time1 = one.getTime();
			long time2 = two.getTime();
			long diff ;
			if(time1<time2) {
				diff = time2 - time1;
			} else {
				diff = time1 - time2;
			}
			days = diff / (1000 * 60 * 60 * 24);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return days;
	}

	/**
	 * 两个时间相差距离多少天多少小时多少分多少秒
	 * @param str1 时间参数 1 格式：1990-01-01 12:00:00
	 * @param str2 时间参数 2 格式：2009-01-01 12:00:00
	 * @return long[] 返回值为：{天, 时, 分, 秒}
	 */
	public static long[] getDistanceTimes(String str1, String str2) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date one;
		Date two;
		long day = 0;
		long hour = 0;
		long min = 0;
		long sec = 0;
		try {
			one = df.parse(str1);
			two = df.parse(str2);
			long time1 = one.getTime();
			long time2 = two.getTime();
			long diff ;
			if(time1<time2) {
				diff = time2 - time1;
			} else {
				diff = time1 - time2;
			}
			day = diff / (24 * 60 * 60 * 1000);
			hour = (diff / (60 * 60 * 1000) - day * 24);
			min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
			sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long[] times = {day, hour, min, sec};
		return times;
	}

	/**
	 * 两个时间相差距离多少天多少小时多少分多少秒
	 * @param date1 时间参数 1 格式：1990-01-01 12:00:00
	 * @param date2 时间参数 2 格式：2009-01-01 12:00:00
	 * @return long[] 返回值为：{天, 时, 分, 秒}
	 */
	public static long[] getDistanceTimes(Date date1, Date date2) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long day = 0;
		long hour = 0;
		long min = 0;
		long sec = 0;
		try {
			long time1 = date1.getTime();
			long time2 = date2.getTime();
			long diff ;
			if(time1<time2) {
				diff = time2 - time1;
			} else {
				diff = time1 - time2;
			}
			day = diff / (24 * 60 * 60 * 1000);
			hour = (diff / (60 * 60 * 1000) - day * 24);
			min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
			sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		} catch (Exception e) {
			e.printStackTrace();
		}
		long[] times = {day, hour, min, sec};
		return times;
	}

	/**
	 * 两个时间相差距离多少天多少小时多少分多少秒
	 * @param str1 时间参数 1 格式：1990-01-01 12:00:00
	 * @param str2 时间参数 2 格式：2009-01-01 12:00:00
	 * @return String 返回值为：xx天xx小时xx分xx秒
	 */
	public static String getDistanceTime(String str1, String str2) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date one;
		Date two;
		long day = 0;
		long hour = 0;
		long min = 0;
		long sec = 0;
		try {
			one = df.parse(str1);
			two = df.parse(str2);
			long time1 = one.getTime();
			long time2 = two.getTime();
			long diff ;
			if(time1 < time2) {
				diff = time2 - time1;
			} else {
				diff = time1 - time2;
			}
			day = diff / (24 * 60 * 60 * 1000);
			hour = (diff / (60 * 60 * 1000) - day * 24);
			min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
			sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return day + "天" + hour + "小时" + min + "分" + sec + "秒";
	}

	/**
	 * 两个时间相差距离多少天多少小时多少分多少秒
	 * @param one 时间参数 1 格式：1990-01-01 12:00:00
	 * @param two 时间参数 2 格式：2009-01-01 12:00:00
	 * @return String 返回值为：xx天xx小时xx分xx秒
	 */
	public static String getDistanceTimeByDate(Date one, Date two) {
		long day = 0;
		long hour = 0;
		long min = 0;
		long sec = 0;
		try {
//			one = df.parse(str1);
//			two = df.parse(str2);
			long time1 = one.getTime();
			long time2 = two.getTime();
			long diff ;
			if(time1 < time2) {
				diff = time2 - time1;
			} else {
				diff = time1 - time2;
			}
			day = diff / (24 * 60 * 60 * 1000);
			hour = (diff / (60 * 60 * 1000) - day * 24);
			min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
			sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		} catch (Exception e) {
			e.printStackTrace();
		}
		String ret = "";
		if(day > 0){
			ret = day + "天" + hour + "小时" + min + "分" + sec + "秒";
		} else if(day == 0 && hour > 0){
			ret = hour + "小时" + min + "分" + sec + "秒";
		} else if(day == 0 && hour == 0&& min > 0){
			ret = min + "分" + sec + "秒";
		} else if(day == 0 && hour == 0&& min == 0 && sec > 0){
			ret = sec + "秒";
		}
		return ret;
	}


	/**
	 *  获取几个月之后的时间
	 * @param monthCount
	 * @return
	 */
	public static String getAfterMonthDate(int monthCount){
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, monthCount);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = cal.getTime();
		return sdf.format(date);
	}

	/**
	 * 获取本月第一天
	 * @return
	 */
	public static Date getMonthFirstDay(){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		calendar.set(Calendar.HOUR_OF_DAY,0);
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND,0);
		return calendar.getTime();
	}

	public static String getMonthFirstDayStr(){
		return getDayByDate(getMonthFirstDay());
	}

	/**
	 * 获取本月最后一天
	 * @return
	 */
	public static Date getMonthLastDay(){
		Calendar calendar2 = Calendar.getInstance();
		calendar2.setTime(new Date());
		calendar2.set(Calendar.DAY_OF_MONTH, calendar2.getActualMaximum(Calendar.DAY_OF_MONTH));
		calendar2.set(Calendar.HOUR_OF_DAY,23);
		calendar2.set(Calendar.MINUTE,59);
		calendar2.set(Calendar.SECOND,59);
		return calendar2.getTime();
	}

	public static String getMonthLastDayStr(){
		return getDayByDate(getMonthLastDay());
	}


    /**
     * 得到n天之后是周几
     * @param days
     * @return
     */
    public static String getAfterDayWeek(int days) {
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.add(Calendar.DATE, days); // 日期减 如果不够减会将月变动
        Date date = canlendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("E");
        String dateStr = sdf.format(date);
        return dateStr;
    }

	/**
	 * 得到 天数 的时间  （负数查前几天的  正数 查后几天的）
	 * @param days
	 * @param isLimitTime  0 - 时分秒不动 1 - 取0点时间  2 - 取23点59分59秒 时间
	 * @return
	 */
	public static Date getDateByDays(int days, int isLimitTime) {
		Calendar canlendar = Calendar.getInstance(); // java.util包
		canlendar.add(Calendar.DATE, days); // 日期减 如果不够减会将月变动
		if(isLimitTime == 1){
			canlendar.set(Calendar.HOUR_OF_DAY,0);
			canlendar.set(Calendar.MINUTE,0);
			canlendar.set(Calendar.SECOND,0);
		}else if(isLimitTime == 2){
			canlendar.set(Calendar.HOUR_OF_DAY,23);
			canlendar.set(Calendar.MINUTE,59);
			canlendar.set(Calendar.SECOND,59);
		}
		Date date = canlendar.getTime();
		return date;
	}


	/**
	 *  获取本周的开始日期
	 * @return
	 */
	public static String getDateByWeek(){
		// 创建 Calendar 对象
		Calendar calendar = Calendar.getInstance();

		// 设置为本周第一天（星期日）
		int firstDayOfWeek = calendar.getFirstDayOfWeek();
		if (calendar.get(Calendar.DAY_OF_WEEK) != firstDayOfWeek) {
			calendar.add(Calendar.DATE, -1);
		}

		// 格式化输出日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String startDateStr = sdf.format(calendar.getTime());
		System.out.println("本周开始时间：" + startDateStr);

		// 将 Calendar 对象加上6天得到本周最后一天（星期六）
		calendar.add(Calendar.DATE, 6);
		String endDateStr = sdf.format(calendar.getTime());
		System.out.println("本周结束时间：" + endDateStr);
		return "";
	}

	// 获得本周一0点时间
	// 获得本周一0点时间
	public static Date getTimesWeekMorning() {
		Calendar cal = Calendar.getInstance();
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return cal.getTime();
	}

	public static String getWeekBegin() {
//		Calendar cal = Calendar.getInstance();
//		cal.setTime(new Date());
////		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
//		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);


		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
		Calendar cld = Calendar.getInstance(Locale.CHINA);
		cld.setFirstDayOfWeek(Calendar.MONDAY);//以周一为首日
		cld.setTimeInMillis(System.currentTimeMillis());//当前时间

		cld.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);//周一
		System.out.println("周一 " + df.format(cld.getTime()));

		String endDateStr = df.format(cld.getTime());
		System.out.println("本周开始时间：" + endDateStr);
		return endDateStr;
	}

	// 获得本周日24点时间
	public static Date getTimesWeekNight() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getTimesWeekMorning());
		cal.add(Calendar.DAY_OF_WEEK, 6);
		return cal.getTime();
	}

	public static String getWeekEnd() {
//		Calendar cal = Calendar.getInstance();
//		cal.setTime(getTimesWeekMorning());
//		cal.add(Calendar.DAY_OF_WEEK, 6);
//		// 格式化输出日期
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");



		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
		Calendar cld = Calendar.getInstance(Locale.CHINA);
		cld.setFirstDayOfWeek(Calendar.MONDAY);//以周一为首日
		cld.setTimeInMillis(System.currentTimeMillis());//当前时间

//		cld.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);//周一
//		System.out.println("周一 " + df.format(cld.getTime()));

		cld.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);//周日
		System.out.println("周日 " + df.format(cld.getTime()));



		String endDateStr = df.format(cld.getTime());
		System.out.println("本周结束时间：" + endDateStr);
		return endDateStr;
	}


	/**
	 *  获取多少秒之后的时间
	 * @return
	 */
	public static Date getAfterSeconds(int seconds){
		Calendar canlendar = Calendar.getInstance(); // java.util包
		canlendar.setTime(new Date());
		canlendar.set(Calendar.SECOND,seconds);
		return canlendar.getTime();
	}

	/**
	 *  获取2个时间的相差的分钟数
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static long getMinutesByTowDate(Date date1,Date date2){

		LocalDateTime startTime = DateUtil.dataToLocalDateTime(date1);

		LocalDateTime nowTime = DateUtil.dataToLocalDateTime(date2);

		Duration duration = Duration.between(startTime, nowTime);
		long times = duration.toMinutes();
		return  times;
	}


	/**
	 *  获取多少秒之后的时间
	 * @return
	 */
	public static Date getAfterSecondsByOld(Date date,int seconds){
		Calendar canlendar = Calendar.getInstance(); // java.util包
		canlendar.setTime(date);
		canlendar.add(Calendar.SECOND,seconds);
		return canlendar.getTime();
	}


	/**
	 *  获取2个月份之间的所有月份
	 * @param y1
	 * @param y2
	 * @return
	 */
	public static List<String> getMonthsByTwoDate(String y1,String y2){
//		String y1 = "2016-02";// 开始时间
//		String y2 = "2019-12";// 结束时间
		List<String> list = new ArrayList<>();

		try {
			Date startDate = new SimpleDateFormat("yyyy-MM").parse(y1);
			Date endDate = new SimpleDateFormat("yyyy-MM").parse(y2);

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(startDate);
			// 获取开始年份和开始月份
			int startYear = calendar.get(Calendar.YEAR);
			int startMonth = calendar.get(Calendar.MONTH);
			// 获取结束年份和结束月份
			calendar.setTime(endDate);
			int endYear = calendar.get(Calendar.YEAR);
			int endMonth = calendar.get(Calendar.MONTH);
			//
			for (int i = startYear; i <= endYear; i++) {
				String date = "";
				if (startYear == endYear) {
					for (int j = startMonth; j <= endMonth; j++) {
						if (j < 9) {
							date = i + "-0" + (j + 1);
						} else {
							date = i + "-" + (j + 1);
						}
						list.add(date);
					}

				} else {
					if (i == startYear) {
						for (int j = startMonth; j < 12; j++) {
							if (j < 9) {
								date = i + "-0" + (j + 1);
							} else {
								date = i + "-" + (j + 1);
							}
							list.add(date);
						}
					} else if (i == endYear) {
						for (int j = 0; j <= endMonth; j++) {
							if (j < 9) {
								date = i + "-0" + (j + 1);
							} else {
								date = i + "-" + (j + 1);
							}
							list.add(date);
						}
					} else {
						for (int j = 0; j < 12; j++) {
							if (j < 9) {
								date = i + "-0" + (j + 1);
							} else {
								date = i + "-" + (j + 1);
							}
							list.add(date);
						}
					}

				}

			}

			// 所有的月份已经准备好
			//System.out.println(list);
//			for(int i = 0;i < list.size();i++){
//				System.out.println(list.get(i));
//				listDates.add()
//			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}



    public static void main(String[] args) {

//		System.out.println(DateUtil.getDateByDays(new Date(),6));

//    	System.out.println(getDays());
//
//    	int  dayOrNight = dayOrNight();
//		System.out.println("dayOrNight: "+ dayOrNight);
//
//		String s = getAfterDayDate(10);
//		System.out.println("s: "+ s);

//		Date date = getDateByDays(-3,2);
//		System.out.println(date);

//		System.out.println(String.format("%020d",new Date().getTime()));
//
//		getMonthsByTwoDate("2022-01","2022-12");

//		System.out.println("5天前的时间： " + getBeforeDayDate(-5));
//
//
//		Calendar calendar = Calendar.getInstance();
////		calendar.setTime(new Date());
//		calendar.set(Calendar.HOUR_OF_DAY,1);
//		calendar.set(Calendar.MINUTE,0);
//		calendar.set(Calendar.SECOND,0);
//		calendar.add(Calendar.DATE,-1);
//		String yesTodayDate = DateUtil.getDayByDate(calendar.getTime());//昨天的日期
//
//		System.out.println("yesTodayDate: " + calendar.getTime());
//
//
//		Calendar calendar1 = Calendar.getInstance();
////		calendar1.setTime(new Date());
//		calendar1.add(Calendar.DAY_OF_MONTH,-1);
//		String yesTodayDate1 = DateUtil.getDayByDate(calendar1.getTime());//昨天的日期
//
//		System.out.println("yesTodayDate1: " + yesTodayDate1);

//		getDateByWeek();

//		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
//		Calendar cld = Calendar.getInstance(Locale.CHINA);
//		cld.setFirstDayOfWeek(Calendar.MONDAY);//以周一为首日
//		cld.setTimeInMillis(System.currentTimeMillis());//当前时间
//
//		cld.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);//周一
//		System.out.println("周一 " + df.format(cld.getTime()));
//
//		cld.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);//周日
//		System.out.println("周日 " +df.format(cld.getTime()));


		Date date = new Date(1755854730409L);
		System.out.println(date);

//		System.out.println(	getDateByString("20240109121134"));
//		System.out.println(getMonthFirstDayStr());
//		System.out.println(getMonthLastDayStr());

//		Date date = DateUtil.getAfterSecondsByOld(new Date(), -10500);
//		LocalDateTime startTime = DateUtil.dataToLocalDateTime(date);
//
//		LocalDateTime nowTime = DateUtil.dataToLocalDateTime(new Date());
//
//		Duration duration = Duration.between(startTime, nowTime);
//		long times = duration.toMinutes();
//
//		System.out.println("相差分钟数：" +times);


//		Date date = getDateTimeByStr("20141111170043");
//		System.out.println("date" + date);

//    	List<String> days = getDaysBetween(6,"yyyy-MM-dd");
//
//
//
//		int weeday = DateUtil.getWeekIndexOfDate(new Date());
//		System.out.println("weeday: " + weeday);

    }

}
